package edu.xidian.bdilab.jrbigdata.service.admin.enterpriseadmin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.admin.AdminCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;

import edu.xidian.bdilab.jrbigdata.domain.EnterpriseReq;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseReqMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.admin.enterpriseadmin.enterpriseadminService;
import org.springframework.beans.factory.annotation.Autowired;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import  edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import org.springframework.stereotype.Service;
import  edu.xidian.bdilab.jrbigdata.mapper.EnterpriseUserMapper;
import java.awt.*;
import java.util.List;
@Service
public class enterpriseadminServiceimpl implements enterpriseadminService {

    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    EnterpriseUserMapper enterpriseUserMapper;
    @Autowired
    EnterpriseReqMapper enterpriseReqMapper;
    @Autowired
    ProductRequestMapper productRequestMapper;

    @Override
    public ResponseResult enterpriseManage_check() throws Exception
    {
        ResponseResult responseResult=new ResponseResult();
        try {
            List<EnterpriseInfo> enterpriseInfoList=enterpriseInfoMapper.selectList(null);
            if(enterpriseInfoList.size()!=0){
                responseResult.setData(enterpriseInfoList);
                responseResult.setSuccess(Boolean.TRUE);
                responseResult.setCode(AdminCode.SelectSUCCESS.getCode());
                responseResult.setMessage(AdminCode.SelectSUCCESS.getMessage());
            }else{
                responseResult.setCode(AdminCode.NOT_EXIST.getCode());
                responseResult.setMessage(AdminCode.NOT_EXIST.getMessage());
                responseResult.setSuccess(true);
            }
            return responseResult;
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.SelectFailed.getCode());
            responseResult.setMessage(AdminCode.SelectFailed.getMessage());
            return responseResult;
        }

    }
    @Override
    public ResponseResult  enterpriseManage_delete(String socialCode ) throws Exception
    {

        ResponseResult responseResult=new ResponseResult();

        //删除该企业申请金融产品的记录
        QueryWrapper<ProductRequest> productRequestQueryWrapper=new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_social_code",socialCode);
        productRequestMapper.delete(productRequestQueryWrapper);

        //删除该企业的需求
        QueryWrapper<EnterpriseReq> enterpriseReqQueryWrapper=new QueryWrapper<>();
        enterpriseReqQueryWrapper.eq("fk_social_code",socialCode);
        enterpriseReqMapper.delete(enterpriseReqQueryWrapper);

        //删除企业用户
        try {
            int a=enterpriseInfoMapper.deleteById(socialCode);
            if(a!=0)
            {
                int b=enterpriseUserMapper.deleteById(socialCode);
                if(b!=0){
                    responseResult.setSuccess(Boolean.TRUE);
                    responseResult.setCode(AdminCode.DELEATESUCCESS.getCode());
                    responseResult.setMessage(AdminCode.DELEATESUCCESS.getMessage());
                    return responseResult;
                }
            }
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.DELEATEFAILED.getCode());
            responseResult.setMessage(AdminCode.DELEATEFAILED.getMessage());
            return responseResult;
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.DELEATEFAILED.getCode());
            responseResult.setMessage(AdminCode.DELEATEFAILED.getMessage());
            return responseResult;
        }

    }

}

